package com.em.controller;

import com.em.entity.Device;
import com.em.entity.Property;
import com.em.service.DeviceService;
import com.em.service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/device")
public class DeviceController {
    private static String PAGE = "/device";
    private static String REDIRECT_PAGE = "redirect:" + PAGE;

    @Autowired
    private DeviceService service;

    @GetMapping("/list")
    public String list(@RequestParam(value = "name", defaultValue = "") String name,
                       @RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       Model model) {
        int pageSize = 10;
        List<Device> list = service.select(name);
        int totalSize = list.size();
        int start = (pageNumber - 1) * pageSize;
        if (start > totalSize) {
            list = Collections.emptyList();
        } else {
            // 获取分页数据
            list = list.subList(start, Math.min(start + pageSize, totalSize));
        }
        model.addAttribute("list", list);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("totalPages", list.size());
        return PAGE + "/list";
    }

    @PostMapping("/add")
    public String add(@RequestParam String deviceName,
                      @RequestParam String deviceType,
                      @RequestParam String status,
                      @RequestParam String installationDate,
                      @RequestParam String lastMaintenanceDate) {
        Device device = new Device();
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setStatus(status);
        device.setInstallationDate(installationDate);
        device.setLastMaintenanceDate(lastMaintenanceDate);
        service.create(device);
        return REDIRECT_PAGE + "/list";
    }

    @GetMapping("/get/{id}")
    @ResponseBody
    public Device getResident(@PathVariable Integer id) {
        Device entity = service.selectById(id);
        return entity;
    }

    @PostMapping("/update")
    public String update(@RequestParam Integer id,
                         @RequestParam String deviceName,
                         @RequestParam String deviceType,
                         @RequestParam String status,
                         @RequestParam String installationDate,
                         @RequestParam String lastMaintenanceDate) {
        Device device = new Device();
        device.setId(id);
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setStatus(status);
        device.setInstallationDate(installationDate);
        device.setLastMaintenanceDate(lastMaintenanceDate);
        service.update(device);
        return REDIRECT_PAGE + "/list";
    }


    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return REDIRECT_PAGE + "/list";
    }
}
